package mbp.gui.panels;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import mbp.core.ResourceUtility;
import mbp.gui.base.IListPanel;
import mbp.message.utility;

public class UtilityListPanel extends JPanel implements IListPanel
{

	private JTable RcuTable;
	private DefaultTableModel RcuModel;
	
	private List<utility> rcuiList;
	private List<ResourceUtility> rcuList;
	
	/**
	 * Create the panel.
	 */
	public UtilityListPanel() 
	{
		setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);
		
		RcuModel = new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"Type", "Function"
				}
			);
		
		RcuTable = new JTable(){
			@Override
			public boolean isCellEditable(int row, int column) {
		    // set table column uneditable
		    return false;
			}
		  };
	
		RcuTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		RcuTable.setFillsViewportHeight(true);
		RcuTable.setToolTipText("<html>Double click for ResourceUtility details<BR>Press DEL to remove</html>");
		RcuTable.setModel(RcuModel);
		
		scrollPane.setViewportView(RcuTable);
		
		//podwojne klikniecie na tabele i pobranie indeksu zaznaczonego wiersza
		RcuTable.addMouseListener(new MouseAdapter(){
		     public void mouseClicked(MouseEvent e){
		      if (e.getClickCount() == 2){
		         processDoubleClick(RcuTable.getSelectedRow());
		         }
		      }
		     } );
		
		RcuTable.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_DELETE)
				processKeyPressed(RcuTable.getSelectedRow());			
			}
		});


	}
	
	public void processDoubleClick(int index)
	{
		System.out.println(" double click: selected row: " + RcuTable.getSelectedRow());
	}
	
	public void processKeyPressed(int index)
	{
		System.out.println(" key pressed: selected row: " + RcuTable.getSelectedRow());
	}
	
	
	private void reloadInfo()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				int rows = 0;
				do
				{
					rows = RcuModel.getRowCount();
					if(rows>0)
					{
						RcuModel.removeRow(0);
					}
				}while(rows>0);
				
				for(int i=0; i<rcuiList.size(); i++)
				{
					String type = rcuiList.get(i).type;
					String cnd = rcuiList.get(i).expression;
					RcuModel.insertRow(RcuModel.getRowCount(), new Object[]{type, cnd});
					
				}
				
			}
		});
	}
	
	private void reloadObj()
	{
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				int rows = 0;
				do
				{
					rows = RcuModel.getRowCount();
					if(rows>0)
					{
						RcuModel.removeRow(0);
					}
				}while(rows>0);
				
				for(int i=0; i<rcuList.size(); i++)
				{
					String type = rcuList.get(i).getType().getResourceTypeName();
					String cnd = rcuList.get(i).getExrpession();
					RcuModel.insertRow(RcuModel.getRowCount(), new Object[]{type, cnd});
					
				}
				
			}
		});
	}
	
	public JTable getTable()
	{
		return RcuTable;
	}

	@Override
	public void reloadList() {
		if(rcuList != null)
		{
			reloadObj();
		}
		if(rcuiList != null)
		{
			reloadInfo();
		}
		
	}

	@Override
	public <utility> void reloadListInfo(List<utility> infoList) {
		this.rcuiList = (List<mbp.message.utility>) infoList;
		this.rcuList = null;
		reloadInfo();
		
	}

	@Override
	public <ResourceUtility> void reloadListObj(List<ResourceUtility> objList) {
		this.rcuList = (List<mbp.core.ResourceUtility>) objList;
		this.rcuiList = null;
		reloadObj();
		
	}


}
